﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WarehouseWebAPP.code.BLL;
using WarehouseWebAPP.code.DAL;
using WarehouseWebAPP.code.DAL.CustomerTypeDSTableAdapters;
using System.Data;


namespace WarehouseWebAPP.Pages.admin.Edit_Entities
{
    public partial class EditCustomerTypes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ErrorLabel.Visible = false;
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {

            CustomerTypeBLL BLL= new CustomerTypeBLL();
            String Previous = BLL.CustomerTypeGetByNum((Guid)CustomerTypeDetailsView.SelectedValue)[0].customer_type;

                if (Previous != args.Value)
                {
                    try
                    {
                        if (BLL.CustomerTypeGetByName(args.Value).Rows.Count > 0)
                            args.IsValid = false;
                        else
                            args.IsValid = true;
                    }
                    finally
                    {
                        //TODO: Dispose
                    }
                
            }
        }

        protected void CustomerTypeDetailsView_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
        {
            try
            {
                Label Label = (Label)CustomerTypeDetailsView.FindControl("CustomerTypeLabel");
                CustomerTypeBLL BLL = new CustomerTypeBLL();
                Guid guid = BLL.CustomerTypeGetByName(Label.Text)[0].customer_type_num;

                CustomerBLL CBLL = new CustomerBLL();
                if (CBLL.CustomerGetByType(guid).Rows.Count > 0)
                {
                    ErrorLabel.Visible = true;
                    e.Cancel = true;
                }

            }
            finally
            {
                //TODO: Dispose
            }
        }




    }
}
